function t(t) {
    return t && t.__esModule ? t : {
        default: t
    };
}

var a = t(require("../../utils/api")), e = t(require("../../utils/user")), n = t(require("../../utils/event-emitter")), i = getApp(), r = [ {
    top: -2,
    right: -5
}, {
    top: -5,
    right: -8
}, {
    top: -8,
    right: -18
} ];

Component({
    properties: {
        className: {
            type: String,
            observer: function(t) {
                this.setData({
                    className: t
                });
            }
        },
        move: {
            type: Boolean,
            value: !1
        },
        name: String,
        uid: String,
        avatar: String,
        card: Object,
        toCard: Object
    },
    data: {
        accesstoken: ""
    },
    ready: function() {
        var t = this, a = i.globalData, n = a.radarCount, r = a.WX_APP_NAME;
        this.__setCount({
            length: 1,
            count: n || 0
        }), this.setData({
            app: r
        }), this.__unReadsSynGlobal(), this.__getUnreads(), e.default.getTokens(function(a) {
            var n = function(a) {
                var e = a.accesstoken;
                return t.setData({
                    accesstoken: e
                });
            };
            a ? n(a) : e.default.listenToken(n);
        });
    },
    methods: {
        __getUnreads: function() {
            var t = this;
            a.default.chat.message.unreads().then(function(a) {
                var e = a.data.count;
                t.__handleCount(e, !0);
            });
        },
        __unReadsSynGlobal: function() {
            var t = this;
            n.default.addListener("unreads.syn", function(a) {
                var e = a.type, n = a.num, r = +i.globalData.radarCount || 0;
                t.data.count === r && (r = "add" === e ? r + n : r - n), t.__handleCount(r);
            });
        },
        __handleCount: function() {
            var t = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : 0;
            arguments[1];
            console.log(t), t < 0 && (t = 0);
            var a = ("" + t).length;
            i.globalData.radarCount = t, this.__setCount({
                count: t,
                length: a
            });
        },
        __setCount: function(t) {
            var a = t.count, e = void 0 === a ? 0 : a, i = t.length, o = void 0 === i ? 1 : i;
            e = e > 99 ? "99+" : e, this.setData({
                count: e,
                position: r[o - 1]
            }), n.default.emit("radar.unreads.change", {
                count: e
            });
        },
        __orient: function() {
            var t = this.data, a = t.name, e = t.app, n = t.uid, i = t.accesstoken, r = t.card, o = r ? r.cardid : "";
            if (!i) return this.triggerEvent("fail", {
                errMsg: "navigateTo:fail auth deny"
            }, {});
            a ? this.__goChat() : wx.navigateTo({
                url: "/publicPackage/salesradar/withinmsg/withinmsg?app=" + e + "&uid=" + n + "&cardid=" + o
            });
        },
        __goChat: function() {
            var t = this.data, e = t.name, n = t.app, i = t.uid, r = t.card, o = t.toCard, d = r ? r.cardid : "", u = o ? o.cardid : "", s = o ? o.guid : "";
            a.default.chat.chat.create({
                data: {
                    touid: i
                }
            }).then(function(t) {
                var a = t.data, r = a.chatid, o = a.count;
                wx.navigateTo({
                    url: "/publicPackage/salesradar/minichat/minichat?toguid=" + s + "&name=" + e + "&num=" + o + "&chatid=" + r + "&cardid=" + d + "&tocardid=" + u + "&touid=" + i + "&app=" + n + "&view=1"
                });
            });
        },
        __collectFormId: function(t) {
            var e = t.detail.formId, n = getApp().globalData.appid;
            a.default.weapp.collectformid({
                data: {
                    formid: e,
                    app: this.data.app,
                    appid: n
                }
            });
        }
    }
});